﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ComponentModel.Composition;
using UnfriendlyFire.Models;

namespace UnfriendlyFire.Controllers
{
    [Export(typeof(IController))]
    [ExportMetadata("Name", "WeaponType")]
    [PartCreationPolicy(System.ComponentModel.Composition.CreationPolicy.NonShared)]
    public class WeaponTypeController : Controller
    {
        private GameRepository _gameRepository;

        [ImportingConstructor]
        public WeaponTypeController(GameRepository gameRepository)
        {
            _gameRepository = gameRepository;
        }

        public ActionResult Index()
        {
            return View(_gameRepository.AuthoredWeaponTypes);
        }

        public ActionResult Details(int id)
        {
            return View();
        }

        public ActionResult Create()
        {
            return View(new WeaponType());
        } 

        [HttpPost]
        [Authorize]
        public ActionResult Create(WeaponType weaponType)
        {
            if (!ModelState.IsValid)
            {
                return View(weaponType);
            }
            try
            {
                var officer = _gameRepository.Officers.First(o => o.Email == HttpContext.User.Identity.Name);
                weaponType.Author = officer;
                _gameRepository.WeaponTypes.AddObject(weaponType);
                _gameRepository.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View(weaponType);
            }
        }
        
        public ActionResult Edit(int id)
        {
            return View();
        }

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Delete(int id)
        {
            return View();
        }

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
